package com.soft.servlets.emp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.soft.pojo.Emp;
import com.soft.pojo.Result;
import com.soft.service.EmpService;
import com.soft.service.impl.EmpServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/insertEmp")
public class InsertEmpServlet extends HttpServlet {
    private EmpService service = new EmpServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ename = req.getParameter("ename");
        String job = req.getParameter("job");
        Integer deptno = Integer.parseInt(req.getParameter("deptno"));
        Double sal = Double.parseDouble(req.getParameter("sal"));
        Emp emp = new Emp(0,ename,job,deptno,sal);
        service.insertEmp(emp);
        Result result = Result.success();
        //响应
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        resp.setContentType("application/json");
        resp.getWriter().print(json);
    }
}
